/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x;

/**
 *
 * @author andrew
 */

import java.net.*;

import java.io.*;
public class TCPClient{



       ObjectInputStream Sinput;                // to read the socker

        ObjectOutputStream Soutput;        // towrite on the socket

        Socket socket;

        ClientR cr = new ClientR();
        ClientW cw = new ClientW();
        ConsoleRead con = new ConsoleRead();




        TCPClient(int port, String ip) {


                try {

                       socket = new Socket(InetAddress.getByName(ip), port);
                       socket.setKeepAlive(true);

                }

                catch(Exception e) {

                        System.out.println("Error connectiong to server:" + e);

                        return;

               }

                System.out.println("Connection accepted " +

                                socket.getInetAddress() + ":" +

                                socket.getPort());



                /* Creating Data Stream */

                try

                {

                        Sinput  = new ObjectInputStream(socket.getInputStream());

                        Soutput = new ObjectOutputStream(socket.getOutputStream());

                }

               catch (IOException e) {

                        System.out.println("Exception creating new Input/output Streams: " + e);

                        return;

                }
                try
                {
                    cr.start();
                    //cw.start();
                    con.start();
                }
                catch(Exception e)
                {
                    System.out.print(e);
                }
    }
    public class ClientR extends Thread{
        public void run()
        {
            boolean reading = true;
            int counter = 0;
            while(reading)
            {
                try {

                        String response = (String) Sinput.readObject();

                        //System.out.println("From Server: " + response + '\n');
                        if(response.charAt(0)=='1'){
                            String token= new String();
                                    int i = 1;
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    int playerId = Integer.parseInt(token);
                                    token = new String();
                                    while(response.charAt(i)==' ')
                                   {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String playerName = token;
                                    token = new String();

                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }

                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String xS =token;
                                    float x = Float.parseFloat(token);
                                    token = new String();
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String yS = token;
                                    float y = Float.parseFloat(token);
                                    Game.players.get(playerId).Name=playerName;
                                    if(Game.players.get(playerId).x-x>0)
                                        Game.players.get(playerId).image=Game.spriteLeft[counter%3];
                                    else if(Game.players.get(playerId).x-x<0)
                                        Game.players.get(playerId).image=Game.spriteRight[counter%3];
                                    else if(Game.players.get(playerId).x-x==0 && Game.players.get(playerId).y-y>0)
                                        Game.players.get(playerId).image=Game.spriteUp[counter%3];
                                    else if(Game.players.get(playerId).x-x==0 && Game.players.get(playerId).y-y<0)
                                        Game.players.get(playerId).image=Game.spriteFront[counter%3];
                                    Game.players.get(playerId).x=x;

                                    Game.players.get(playerId).y=y;

                                   /* for(int j = 0; j<Game.players.size(); j++)
                                        if(Game.players.get(j).Id==playerId &&playerId!=Game.localPlayerID)
                                        {
                                            Game.players.get(j).Name=playerName;
                                            Game.players.get(j).x = x;
                                            Game.players.get(j).y=y;
                                        }*/
                        } else if(response.charAt(0)=='2'){
                            int i =1;
                            for(i = 1; response.charAt(i)==' '; i++);
                            String token = new String();
                            for(int j = i; j<response.length(); j++)
                            {
                                token +=response.charAt(j);
                            }
                            int pID = Integer.parseInt(token);
                            Game.localPlayerID = pID;
                        } else if(response.charAt(0)=='3'){
                            String token= new String();
                                    int i = 1;
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    int playerId = Integer.parseInt(token);
                                    token = new String();
                                    while(response.charAt(i)==' ')
                                   {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String playerName = token;
                                    token = new String();
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String xS =token;
                                    float x = Float.parseFloat(token);
                                    token = new String();
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String yS = token;
                                    float y = Float.parseFloat(token);
                                   token= new String();
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                   int area = Integer.parseInt(token);
                                    Game.players.add(playerId,new Entity(Game.spriteFront[0], Game.map, x, y, "player",area));
                                    //Game.players.insertElementAt(new Entity(Game.spriteFront[0], Game.map, x, y), playerId);
                                    //Game.players.get(playerId).Name=playerName;
                                    //Game.players.get(playerId).x=x;
                                    //Game.players.get(playerId).y=y;
                        } else if(response.charAt(0)=='4'){
                         int i =1;
                            for(i = 1; response.charAt(i)==' '; i++);
                            String token = new String();
                            for(int j = i; j<response.length(); j++)
                            {
                                token +=response.charAt(j);
                            }
                            int totP = Integer.parseInt(token);
                            Game.totalPlayers = totP;
                        }else if(response.charAt(0)=='5'){
                            String token= new String();
                                    int i = 1;
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    int pID = Integer.parseInt(token);
                                    token= new String();

                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    int area = Integer.parseInt(token);
                                    System.out.print("Player " + pID + " Entering Zone " + area + "\n");
                                    Entity temp = Game.players.get(pID);
                                    temp.area=area;
                                    Game.players.set(pID, temp);
                                }else if(response.charAt(0)=='6'){
                                    String token= new String();
                                    int i = 1;
                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!=' ')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    int pID = Integer.parseInt(token);
                                    token= new String();

                                    while(response.charAt(i)==' ')
                                    {
                                        i++;
                                    }
                                    while(response.charAt(i)!='\0')
                                    {
                                        token+=response.charAt(i);
                                        i++;
                                    }
                                    String chatMessage = token;
                                    System.out.print(Game.players.get(pID).Name +": " + chatMessage + "\n");
                                    //Entity temp = Game.players.get(pID);
                        }

                        //Sinput.reset();
                        counter++;
                }

                catch(Exception e) {

                        System.out.println("Problem retrieving from server: " + e);

                }
            }
        }
    }
    public class ClientW extends Thread{
        public void run(String packet){
                BufferedReader toServerPacket = new BufferedReader(new InputStreamReader(System.in));

                //System.out.println(packet);

                try {

                       Soutput.writeObject(packet);

                        Soutput.flush();

                }

                catch(IOException e) {

                        System.out.println("Error writting to the socket: " + e);

                        return;

            }
      }
    }
    public class ConsoleRead extends Thread{
        public void run(){
            try{
                while(true){
                BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
                String s = bufferRead.readLine();
                String Packet = "6 " + Game.localPlayerID + " " + s + '\0';
                cw.run(Packet);

                }


	}
	catch(IOException e)
	{
		e.printStackTrace();
	}

        }
    }
}
